using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class forums_controls_search_searchform : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            List<DAL.Forum> myforums = DAL.Forums.All();

            DAL.Forum allForum = new DAL.Forum();
            allForum.Name = "All Forums";

            myforums.Insert(0, allForum);
            lstForums.DataSource = myforums;
            lstForums.DataBind();

            lstForums.SelectedValue = "0";
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Helpers.ForumSearchHelper search = new Helpers.ForumSearchHelper();
        search.Keywords = txtKeywords.Text;
        switch (ddlKeywordType.SelectedValue)
        {
            case "0":
                search.KeywordSearchMode = Helpers.ForumSearchHelper.KeywordSearch.EntirePost;
                break;
            case "1":
                search.KeywordSearchMode = Helpers.ForumSearchHelper.KeywordSearch.SubjectsOnly;
                break;
        }

        search.User = DAL.Users.GetByUsername(txtUsername.Text.Trim());
        switch (ddlUsernameType.SelectedValue)
        {
            case "0":
                search.UserSearchMode = Helpers.ForumSearchHelper.UserSearch.ThreadsCreated;
                break;
            case "1":
                search.UserSearchMode = Helpers.ForumSearchHelper.UserSearch.AllPosts;
                break;
        }

        int replies;
        if (Int32.TryParse(txtReplyCount.Text, out replies))
        {
            search.ReplyCount = replies;
        }
        switch (ddlReplyCountType.SelectedValue)
        {
            case "0":
                search.ReplyFilterMode = Helpers.ForumSearchHelper.ReplyFilter.AtLeast;
                break;
            case "1":
                search.ReplyFilterMode = Helpers.ForumSearchHelper.ReplyFilter.AtMost;
                break;
        }

        switch (ddlDateType.SelectedValue)
        {
            case "0":
                search.ThreadDateMode = Helpers.ForumSearchHelper.ThreadDate.AnyDate;
                break;
            case "1":
                search.ThreadDateMode = Helpers.ForumSearchHelper.ThreadDate.OneWeekAgo;
                break;
            case "2":
                search.ThreadDateMode = Helpers.ForumSearchHelper.ThreadDate.OneMonthAgo;
                break;
            case "3":
                search.ThreadDateMode = Helpers.ForumSearchHelper.ThreadDate.ThreeMonthsAgo;
                break;
            case "4":
                search.ThreadDateMode = Helpers.ForumSearchHelper.ThreadDate.SixMonthsAgo;
                break;
            case "5":
                search.ThreadDateMode = Helpers.ForumSearchHelper.ThreadDate.OneYearAgo;
                break;
        }

        switch (ddlDateFilterType.SelectedValue)
        {
            case "0":
                search.DateFilterMode = Helpers.ForumSearchHelper.DateFilter.Newer;
                break;
            case "1":
                search.DateFilterMode = Helpers.ForumSearchHelper.DateFilter.Older;
                break;
        }

        switch (ddlSortType.SelectedValue)
        {
            case "0":
                search.ThreadSortMode = Helpers.ForumSearchHelper.ThreadSort.LastPostDate;
                break;
            case "1":
                search.ThreadSortMode = Helpers.ForumSearchHelper.ThreadSort.ThreadStartDate;
                break;
            case "2":
                search.ThreadSortMode = Helpers.ForumSearchHelper.ThreadSort.Username;
                break;
            case "3":
                search.ThreadSortMode = Helpers.ForumSearchHelper.ThreadSort.Forum;
                break;
        }

        switch (ddlSortDirection.SelectedValue)
        {
            case "0":
                search.ThreadSortDirection = Helpers.ForumSearchHelper.SortDirection.Desc;
                break;
            case "1":
                search.ThreadSortDirection = Helpers.ForumSearchHelper.SortDirection.Asc;
                break;
        }

        if (optThreads.Checked)
        {
            search.DisplayMode = Helpers.ForumSearchHelper.Display.AsThreads;
        }
        else
        {
            search.DisplayMode = Helpers.ForumSearchHelper.Display.AsPosts;
        }

        foreach (ListItem li in lstForums.Items)
        {
            if (li.Selected)
            {
                long id;
                if (Int64.TryParse(li.Value, out id))
                {
                    if (id > 0)
                    {
                        DAL.Forum forum = DAL.Forums.Item(id);
                        if (forum != null)
                        {
                            search.SelectedForums.Add(forum);
                        }
                    }
                    else
                    {
                        search.SelectedForums.Clear();
                        break;
                    }
                }
            }
        }

        Response.Redirect(string.Format("~/forums/searchresults.aspx?{0}", search.GenerateQueryString()));
    }
}
